// pages/userDrugList/userDrugList.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    keyword: '',
    from: '',
    openSwipeCell: false,
    showEmpty: false,
    swipeCellId: "",
    drugList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    console.log(options)
    const from = options.from
    this.setData({
      from: from
    })
    switch (this.data.from) {
      case "search":
        this.setData({
          keyword: options.keyword
        })
        wx.setNavigationBarTitle({
          title: "包含 " + this.data.keyword + " 的药品",
        })
        break

      case "user":
        this.setData({
          openid: options.openid
        })
        wx.setNavigationBarTitle({
          title: this.data.openid + ' 的药品列表',
        })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  async getSearchResult() {
    // 搜索关键字
    const res = await app.call({
      path: '/drug/searchDrug?keyword=' + this.data.keyword,
      method: 'GET'
    })
    console.log(res)
    if (res.code === 0) {
      console.log(res.data)
      if (res.data.searchList.length == 0) {
        Toast.fail("没有包含 " + this.data.keyword + " 的药品！")
      } else {
        this.setData({
          drugList: res.data.searchList
        })
      }
    } else {
      Toast.fail(res.errorMsg);
    }
    this.setData({
      showEmpty: this.data.drugList.length === 0
    })
  },

  async getUserDrugList() {
    // 获取用户药品列表
    const res = await app.call({
      path: '/drug/userDrugList?openid=' + this.data.openid,
      method: 'GET'
    })
    console.log(res)
    if (res.code === 0) {
      console.log(res.data)
      this.setData({
        drugList: res.data.drugList
      })
    } else {
      Toast.fail(res.errorMsg);
    }
    this.setData({
      showEmpty: this.data.drugList.length === 0
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 获取药品列表
    switch (this.data.from) {
      case "search":
        this.getSearchResult();
        break
      case "user":
        this.getUserDrugList();
    }
  },

  onOpenSwipeCell(event) {
    this.setData({
      openSwipeCell: true
    })
    console.log(event)
    const id = event.currentTarget.id;
    console.log("id->" + id)
    this.setData({
      swipeCellId: id
    })
  },

  async onCloseSwipeCell(event) {
    const {
      position,
      instance
    } = event.detail;
    const index = event.currentTarget.dataset.index;
    const drugList = this.data.drugList.slice(); // 获取用药计划列表副本
    const delName = drugList[index].drugName
    switch (position) {
      case 'left':
      case 'cell':
        instance.close();
        break;
      case 'right':
        Dialog.confirm({
          message: '确定删除 ' + delName + ' 吗？',
        }).then(async () => {
          const res = await app.call({
            path: 'drug/deleteDrug?id=' + this.data.swipeCellId,
            method: 'DELETE'
          })
          console.log(res)
          if (res.code === 0) {
            // 获取药品列表
            switch (this.data.from) {
              case "search":
                this.getSearchResult();
                break
              case "user":
                this.getUserDrugList();
            }
            Toast.success(delName + '\n删除成功');
          } else {
            Toast.fail(res.errorMsg);
          }
          console.log(drugList)
          instance.close();
        }).catch(() => {
          // on cancel
        });
        break
    }
    this.setData({
      openSwipeCell: false
    })
  },

  onClickSwipeCell(event) {
    const position = event.detail
    console.log(position)
    if (this.data.openSwipeCell !== true) {
      const id = event.currentTarget.id;
      console.log("id->" + id)
      wx.navigateTo({
        url: '/pages/editDrug/editDrug?from=resDrugList&id=' + id,
      })
    }
  }
})